package io.xdag.block;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.xdag.utils.DateUtils;

/**
 * 存储读取block
 * @author administrarot
 *
 */
public class Storage {
	private static final Logger log = LoggerFactory.getLogger(Storage.class);
	
	private final static String DELIMITER = File.separator;
	
	public final static String STORAGE_DIR0 = "storage%s";
	
	public final static String STORAGE_DIR1 = "storage%s"+DELIMITER+"%02x";
	
	public final static String STORAGE_DIR2 = "storage%s"+DELIMITER+"%02x"+DELIMITER+"%02x";

	public final static String STORAGE_DIR3 = "storage%s"+DELIMITER+"%02x"+DELIMITER+"%02x"+DELIMITER+"%02x";

	public final static String STORAGE_FILE = "storage%s"+DELIMITER+"%02x"+DELIMITER+"%02x"+DELIMITER+"%02x"+DELIMITER+"%02x.dat";

	/**
	 * 格式化参数
	 * @param time
	 * @param dep
	 * @return
	 */
	public static Object[] formatArgs(long time,int dep){
		Object[] args = new Object[dep+1];
		for(int i=0;i<=dep;i++) {
			if(i==0) {
				args[i]="";
			}else {
				if(dep==1) {
					args[i] = (int)((time) >> (40-(i-1)*8));
				}else {
					args[i] = (int)((time) >> (40-(i-1)*8))&0xff;
				}
			}
		}
		return args;
	}
	/**
	 * 根据时间戳创建存储路径,每64秒生成一个新的路径
	 * @param time
	 * @param dep
	 * @return
	 */
	private static void xdagStorageSave(long time) {

		Object[] args = formatArgs(time,4);
		StringBuilder sb = new StringBuilder("");
		sb.append(String.format(STORAGE_DIR0, ""));
		System.out.println(sb);
		sb = new StringBuilder("");
		sb.append(String.format(STORAGE_DIR1, args));
		System.out.println(sb);
		sb = new StringBuilder("");
		sb.append(String.format(STORAGE_DIR2, args));
		System.out.println(sb);
		sb = new StringBuilder("");
		sb.append(String.format(STORAGE_DIR3, args));
		System.out.println(sb);
		sb = new StringBuilder("");
		sb.append(String.format(STORAGE_FILE, args));
		System.out.println(sb);
	}
	
	/**
	 * 
	 */
	private void xdag_load_blocks(long startTime,long endTime) {
		
	}
	public static void main(String[] args) {
//		long l = DateUtils.getTimestamp();
//		System.out.println(l);
//		System.out.println(l /1024);
//		System.out.println((l <<16));
		xdagStorageSave(DateUtils.getTimestamp());
//		System.out.printf(STORAGE_DIR1,"", 0x1);  
	}
}
